<?php

declare(strict_types=1);

namespace app\model;

use app\common\facade\auth\Auth;
use app\common\service\auth\AuthAdminService;
use app\common\service\auth\AuthAgentService;
use app\model\base\BaseTakeoutVajraModel;
use think\db\Query;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id 自增id
 * @property int $agent_id 代理商id
 * @property string $name 轮播图名称
 * @property string $image 轮播图图片
 * @property int $sort 排序，desc
 * @property int $status 状态：1启用 -1禁用
 * @property string $params 跳转参数
 * @property int $is_deleted 是否删除：1是 0否
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property bool $type 1.外卖2.聚合模板 3.品牌权益
 * @property string $discount_logo 标识
 * @property int $subtitle_type 副标题类型 0 未选择 1 显示折扣值 2自定义
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class TakeoutVajra extends BaseTakeoutVajraModel
{
    use SoftDelete;
    protected $deleteTime = 'is_deleted';
    protected $defaultSoftDelete = 0;

    //类别type
    const TYPE_TACKOUT = 1;  //外卖系统
    const TYPE_UNION   = 2;  //本地生活
    const TYPE_COUPON  = 3;  //会员卡券


    //状态
    const STATUS_ACTIVE = 1;  //启用
    const STATUS_CLOSE  = -1; //禁用

    //删除状态
    const ISDELETED_YES = 1;  //已删除
    const ISDELETED_NO = 0;   //未删除

    //副标题类型
    const SUBTITLE_TYPE_ZK = 1;  //显示折扣
    const SUBTITLE_TYPE_ZDY = 2; //自定义

    protected $auth;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->auth = Auth::instance();
    }

    /**
     * @note 轮播图列表
     * @access public
     * @param $params array 参数
     * @return array
     */
    public function getList($params)
    {
        //代理端
        if ($this->auth instanceof AuthAgentService){
            $params['agent_id'] = Auth::agentId();
        }

        //后台
        if ($this->auth instanceof AuthAdminService){
            $params['agent_id'] = 0;
        }
        return self::query()->withSearch(['agent_id', 'type', 'status'], $params)
            ->order('sort', 'desc')
            ->paginate();
    }

    /**
     * 代理搜索
     */
    public function searchAgentIdAttr($query, $value, $data)
    {
        /**
         * @var Query $query
         */
        //代理需要看到自己的并且看到总后台设置的
        if ($this->auth instanceof AuthAgentService){
            $query->whereRaw('agent_id = '.$value.' OR agent_id = 0');
        } else {
            $query->where('agent_id', 0);
        }
    }

    /**
     * 类型搜索
     */
    public function searchTypeAttr($query, $value, $data)
    {
        if ($value && in_array($value, [self::TYPE_TACKOUT, self::TYPE_UNION, self::TYPE_COUPON])) {
            $query->where('type', $value);
        }
    }

    /**
     * 状态搜索
     */
    public function searchStatusAttr($query, $value, $data)
    {
        if ($value && in_array($value, [self::STATUS_ACTIVE, self::STATUS_CLOSE])) {
            $query->where('status', $value);
        }
    }

    /**
     * 新增/编辑
     */
    public function doSave($params)
    {
        $param = array();
        if (isset($params['jump_type'])) {
            $param['jump_type'] = $params['jump_type'];
        }
        if (isset($params['path'])) {
            $param['path'] = $params['path'];
        }
        if (isset($params['discount_logo'])) {
            $param['discount_logo'] = $params['discount_logo'];
        }
        if (isset($params['subtitle_type'])) {
            $param['subtitle_type'] = $params['subtitle_type'];
        }
        if (isset($params['subtitle'])) {
            $param['subtitle'] = $params['subtitle'];
        }
        if ((!isset($params['image']) || empty($params['image'])) && $params['jump_type'] == 5) {
            $image = CouponBrand::query()->where('id', $params['brand_id'])->value('image_url');
        }
        $data = [
            'agent_id' => $params['agent_id']??0,
            'name'     => $params['name'],
            'image'    => $image??$params['image'],
            'params'   => json_encode($param),
            'sort'     => $params['sort'],
            'type'     => $params['type'],
        ];
        if (isset($params['id']) && !empty($params['id'])) {
//            $info = self::query()->where('id', $params['id'])->find();
//            if (!$info) {
//                return fail('修改失败，数据不存在');
//            }
            $data['id'] = $params['id'];
            self::update($data);
            return success('修改成功');
        } else {
            self::create($data);
            return success('新增成功');
        }

    }
}
